
#pragma once

static INLINE void test_rules1( AllocContext * c )
{
    RuleEngineContext * re = create_rule_engine( c );
    EvalContext *       eval = create_evaluator( c );
    List *              load_call = alloc_listv(c,
                (Obj *)alloc_id(c, "load", alloc_id(c, "System", 0)),
                g_nil_obj,
                alloc_id(c, "verbose", 0),
                g_t_obj,
                0
                );

    /* this is not necessary, but will help us to use more functions */
    set_default_functions( eval );

    set_evaluator_callback( eval, rp_err_callback );

    if( add_rule_engine_functions(eval, re) )
    {
        // load test file
        list_set_at( load_call, 1, (Obj *)alloc_str(c, "../../tests/sqroot.txt") );
        evaluate( eval, (Obj *)load_call );

        fputs( "launch evaluator loop from stdin...\n", stderr );
        list_set_at( load_call, 1, (Obj *)alloc_id(c, "stdin", 0) );
        for( ;; )
        {
            Obj * evaluation_result = evaluate( eval, (Obj *)load_call );

            if( 0 == evaluation_result )
            {
                ErrResult err = get_evaluator_last_error( eval );
                if( QL_ERR_STOP == err )
                {
                    break;
                }

                if( 0 != ask_yesno("Would you like to continue evaluation?") )
                {
                    break;
                }
            }
        }
    }
    else
    {
        fprintf( stderr, "Can't add rule engine functions!!!" );
    }

    free_evaluator( eval );
    free_rule_engine( re );
}

